package com.carlinfo.stu_ques.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.users.dao.IGrade;
import com.carlinfo.stu_ques.users.dao.IStudentsDao;
import com.carlinfo.stu_ques.users.pojo.Grade;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.Istudentsservice;

public class StudentsService implements Istudentsservice
{
	private IStudentsDao student;
	private IGrade gradedaoimpl;

	@Override
	public List<Students> selectStudentsListDao()
	{
		List<Students> list = this.student.selectListDao();
		for (Students students : list)
		{
			double level = students.getLevel();
			List<Grade> selectListDao = this.gradedaoimpl.selectListDao();
			for (Grade grade : selectListDao)
			{
				double stNum = grade.getStNum();
				double edNum = grade.getEdNum();
				if(level<edNum&&level>stNum) 
				{
					String grading = grade.getGrading();
					students.setLevelName(grading);
				}
			}
		}
		return list;
	}

	@Override
	public void savaStudentsOneDao(Students student)
	{
		this.student.savaOneDao(student);
	}

	@Override
	public void updateStudentsOneDao(Students student)
	{
		this.student.updateOneDao(student);
	}

	@Override
	public void delectStudentsOneDao(Students student)
	{
		this.student.deleteOneDao(student);
	}

	@Override
	public Students selectStudentsOneDao(Map<String, Object> OneMap)
	{
		Students student = this.student.selectOneDao(OneMap);
		return student;
	}

	@Override
	public List<Grade> selectGradeListDao()
	{
		List<Grade> list = this.gradedaoimpl.selectListDao();
		return list;
	}

	@Override
	public void savaGradeOneDao(Grade grade)
	{
		this.gradedaoimpl.savaOneDao(grade);
	}

	public boolean askIsStudentService(Students students)
	{
		if (students == null)
		{

			return false;
		}
		int askCount = 0;
		/* 断言他们都相等 */
		boolean askFlag = true;
		int count = students.getTotalCount();
		List<Students> list = this.selectStudentsListDao();
		for (Iterator iterator = list.iterator(); iterator.hasNext();)
		{
			Students stuTemp = (Students) iterator.next();
			if (stuTemp.getTotalCount() > askCount)
			{
				askCount = stuTemp.getTotalCount();
			}
			if (count != stuTemp.getTotalCount())
			{
				askFlag = false;
			}
		}
		if (askCount == 0 || students.getTotalCount() < askCount || askFlag)
		{
			return true;
		}
		return false;
	}

	@Override
	public void updateGradeOneDao(Grade grade)
	{
		this.gradedaoimpl.updateOneDao(grade);
	}

	@Override
	public void delectGradeOneDao(Grade grade)
	{
		this.gradedaoimpl.deleteOneDao(grade);
	}

	@Override
	public Grade selectGradeOneDao(Map<String, Object> OneMap)
	{
		Grade grade = this.gradedaoimpl.selectOneDao(OneMap);
		return grade;
	}

	@Override
	public String printTitleStudentService()
	{
		String result = "id\t" + " 的姓名\t" + " 性别\t" + " 生日\t\t" + " 大学\t" + " 段位\t" + " 创建时间\t\t\t" + " 修改时间\t\t\t"
				+ "总次数\t" + " 总错误次数\t" + " 总正确率\t" + "一天的次数\t" + "一天正确的次数\t" + "一天错误的次数\t" + "一天的正确率";
		return result;
	}

	@Override
	public String printTitleGradeService()
	{
		String result = "id\t名字\t段位\t起始分数\t结束分数\t创建时间\t\t结束时间";
		return result;
	}

	@Override
	public int selectStudentMaxIdService()
	{
		return this.student.selectMaxIdDao();
	}

	public void setStudent(IStudentsDao student)
	{
		this.student = student;
	}

	public void setGradedaoimpl(IGrade gradedaoimpl)
	{
		this.gradedaoimpl = gradedaoimpl;
	}
}
